//tetr qp2 level4 qwq
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1.4e5+5;
int n, m, l, T, a[N], q[N], ok[25][N], ans[N], lg[N];
string vs[35];
struct node{
	int id, t2, sum, now, spc;
	bool operator <(node X)const{
		if(t2==X.t2)return id<X.id;
		return t2<X.t2;
	}
};
vector <node> v;
inline void dfs(int t2, int id, int R){
	if(ok[t2][id]!=-1){
		v.push_back({id,t2,ok[t2][id]+1,a[ok[t2][id]],0});
		return;
	}
	if(id*(1ll<<t2)>R){
		v.push_back({id,t2,((id*(1ll<<t2))+(id+1)*(1ll<<t2)-1)*(1ll<<t2)/2+(1ll<<t2),-1,0});
		return;
	}
	if(!t2)return;
	dfs(t2-1,id*2,R);
	dfs(t2-1,id*2+1,R);
}
inline node add(node a, node b){
	if(a.id>b.id)swap(a,b);
	// cout<<a.id<<' '<<b.id<<endl;
	a.t2++;b.t2++;
	a.id/=2;b.id/=2;
	if(vs[a.t2][a.id]=='0'){
		if(a.now>=a.t2)return a;
		return b;
	}
	else{
		if(b.now>=a.t2)return b;
		return a;
	}
}
inline int calc(int x, int y, int X, int Y){
	if(X>Y)swap(X,Y),swap(x,y);
	x/=(1ll<<(Y-X));
	return lg[y^x]+Y;
}
signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=0; i<n; i++)cin>>a[i];
	for(int i=0; i<m; i++)cin>>q[i],q[i]--;
	for(int i=1; i<N; i++)lg[i]=lg[i/2]+1;
	int zc=1;
	while(zc<=n)zc*=2,l++;
	for(int i=1; i<=l; i++)cin>>vs[i];
	cin>>T;
	while(T--){
		memset(ans,0,sizeof ans);
		int xx[4];
		memset(ok,-1,sizeof ok);
		cin>>xx[0]>>xx[1]>>xx[2]>>xx[3];
		for(int i=0; i<n; i++)a[i]^=xx[(i+1)%4];
		int L=0;
		for(int i=0; i<n; i++){
			// cout<<i<<endl;
			if(i>=(1ll<<L))L++;
			ok[0][i]=i;
			int id=i, now=a[i], t2=0;
			while(t2<L){
				t2++;
				int op=id^1;
				if(id%2){
					if(vs[t2][id/2]=='1'){
						if(a[ok[t2-1][id]]>=t2)ok[t2][id/2]=ok[t2-1][id];
						else ok[t2][id/2]=ok[t2-1][id^1];
					}
					else{
						if(a[ok[t2-1][id^1]]>=t2)ok[t2][id/2]=ok[t2-1][id^1];
						else ok[t2][id/2]=ok[t2-1][id];
					}
				}
				else break;
				id/=2;
			}
			v.clear();
			dfs(L,0,i);
			// if(i==2){
			// 	for(auto y:v)cout<<y.id<<' '<<y.now<<' '<<y.sum<<' '<<y.t2<<'\n';
			// 	cout<<endl;
			// }
			sort(v.begin(),v.end());
			int ii=i;
			for(int i=0; i<v.size(); i++){
				auto V=v;
				for(int j=0; j<v.size(); j++){
					if(v[j].now==-1&&j==i)v[j].now=1e9;
					else if(v[j].now==-1)v[j].now=calc(v[j].id,v[i].id,v[j].t2,v[i].t2)-1;
				}
				// if(i==2&&ii==2){

				// 	for(auto y:v)cout<<y.id<<' '<<y.now<<' '<<y.sum<<' '<<y.t2<<'\n';
				// 	cout<<endl;
				// }
				v[i].spc=1;
				for(int j=0; j<v.size()-1; j++){
					v[j+1]=add(v[j],v[j+1]);
				}
				if(v[v.size()-1].spc){
					ans[ii]+=v[i].sum;
				}
				v=V;
			}
			// cout<<ans[i]<<'\n';
		}
		int anss=0;
		for(int i=0; i<m; i++)anss^=((i+1)*ans[q[i]]);
		cout<<anss<<'\n';
		for(int i=0; i<n; i++)a[i]^=xx[(i+1)%4];
	}
	return 0;
}